package com.gdyx.app.web.svc.impl;

import com.gdyx.app.web.dao.MpsDeptRoleDao;
import com.gdyx.app.web.model.MpsDeptRole;
import com.gdyx.app.web.svc.MpsDeptRoleSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Y_Coffee on 2017/8/3.
 */
@Service
public class MpsDeptRoleSvcImpl implements MpsDeptRoleSvc {
    @Autowired
    MpsDeptRoleDao mpsDeptRoleDao;

    @Override
    public void save(MpsDeptRole o) {
        mpsDeptRoleDao.save(o);
    }

    @Override
    public void saveOrUpdate(MpsDeptRole o) {
        mpsDeptRoleDao.saveOrUpdate(o);
    }

    @Override
    public void delete(String id) {
        mpsDeptRoleDao.delete(id);
    }

    @Override
    public void update(MpsDeptRole o) {
        mpsDeptRoleDao.update(o);
    }

    @Override
    public List<MpsDeptRole> findDeptRoleByRid(String id) {
        return mpsDeptRoleDao.findDeptRoleByRid(id);
    }

    @Override
    public List<MpsDeptRole> findDeptRoleByDid(String id) {
        return mpsDeptRoleDao.findDeptRoleByDid(id);
    }

    @Override
    public void deleteByDid(String id) {
        mpsDeptRoleDao.deleteByDid(id);
    }

    @Override
    public void deleteByRid(String id) {
        mpsDeptRoleDao.deleteByRid(id);
    }

    @Override
    public List<MpsDeptRole> findDeptRoles(Map<String, Object> map) {
        return mpsDeptRoleDao.findDeptRoles(map);
    }

    @Override
    @Transactional
    public void setDept(String ids,String roleId,String Ownerid) {
        if(StringUtils.isEmpty(ids)){
            mpsDeptRoleDao.deleteByRid(roleId);
            return;
        }
        String[] deptIds = ids.split(",");
        List<String> deptIdslist=Arrays.asList(deptIds);
        //角色下关联的部门
        List<MpsDeptRole> deptRoleByRids = mpsDeptRoleDao.findDeptRoleByRid(roleId);
        //删除传入ID以外的数据
        for (MpsDeptRole mpsDeptRole : deptRoleByRids) {
            if(!deptIdslist.contains(mpsDeptRole.getDeptId())){
                mpsDeptRoleDao.deleteByDid(mpsDeptRole.getDeptId());
            }
        }
        //添加或者修改
        for (String deptId : deptIds) {
            MpsDeptRole mpsDeptRole = mpsDeptRoleDao.findDeptRolesByDeptIdAndRoleId(deptId,roleId);
            if(mpsDeptRole!=null){
                //update
                mpsDeptRole.setModifyOwner(Ownerid);
                mpsDeptRole.setModifyTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                mpsDeptRoleDao.update(mpsDeptRole);
            }else{
                //create
                MpsDeptRole m=new MpsDeptRole();
                m.setId(UUID.randomUUID().toString().toUpperCase().trim());
                m.setDeptId(deptId);
                m.setRoleId(roleId);
                m.setCreateOwner(Ownerid);
                m.setCreateTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                mpsDeptRoleDao.save(m);
            }
        }
    }
}
